/**
 * @author Fang Lu, fanglupku@gmail.com
 * 2011-6-8
 */
package com.autotag;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import com.autotag.entity.NPEntity;
import com.autotag.nlp.GramStat;
import com.autotag.train.ManualTagReader;
import com.autotag.train.TrainTerm;

import fanglu.util.FileDealer;

public class DomainTagGenerator {
	public static void main(String args[]) {
//		generateTrainData(
//				"C:/Users/ftc/Desktop/ICSOC/final_data/serviceList(1-150).txt",
//				"C:/Users/ftc/Desktop/ICSOC/final_data/term.txt",
//				"train/model",
//				"C:\\Users\\ftc\\Desktop\\ICSOC\\WSDL_FILE\\result\\result.txt");
//		generateTestData(
//				"C:/Users/ftc/Desktop/ICSOC/final_data/term.txt",
//				"train/test_data.txt",
//				"C:\\Users\\ftc\\Desktop\\ICSOC\\WSDL_FILE\\result\\result.txt"
//				);
		HashMap<String, Integer> uuidMapLine = new HashMap<String, Integer>();
		HashMap<Integer, String> lineMapUUID = new HashMap<Integer, String>();
		initLineMapUUID("train/linemapuuid.txt", uuidMapLine, lineMapUUID);
		initTrainData("C:/Users/ftc/Desktop/ICSOC/final_data/serviceList(1-150).txt", 
				"train/t", "E:/svm/test_data.scale", uuidMapLine, lineMapUUID);
	}
	
	public static void initTrainData(String manualFileName,
			String trainDataFileName, String scaleFileName,
			HashMap<String, Integer> uuidMapLine, HashMap<Integer, String> lineMapUUID) {
		HashMap<String, ArrayList<String>> uuidMapFunc = new HashMap<String, ArrayList<String>>();
		HashMap<String, ArrayList<String>> uuidMapDomain = new HashMap<String, ArrayList<String>>();
		ManualTagReader
				.readTag(manualFileName, 150, uuidMapFunc, uuidMapDomain);
		HashMap<String, Integer> domainMapIndex = new HashMap<String, Integer>();
		HashMap<Integer, HashSet<String>> lineMapDomainSet = new HashMap<Integer, HashSet<String>>();
		HashSet<Integer> lineSet = new HashSet<Integer>();
		Iterator<String> itor_uuid = uuidMapDomain.keySet().iterator();
		int index = 0;
		while (itor_uuid.hasNext()) {
			String thisUUID = itor_uuid.next();
			lineSet.add(uuidMapLine.get(thisUUID));
			ArrayList<String> thisDomainList = uuidMapDomain.get(thisUUID);
			for (int i = 0; i < thisDomainList.size(); ++i) {
				if (!domainMapIndex.containsKey(thisDomainList.get(i))){
					domainMapIndex.put(thisDomainList.get(i), index);
					System.out.println(thisDomainList.get(i));
					++ index;
				}
			}
		}
		
		itor_uuid = uuidMapDomain.keySet().iterator();
		while (itor_uuid.hasNext()) {
			String thisUUID = itor_uuid.next();
			ArrayList<String> thisDomainList = uuidMapDomain.get(thisUUID);
			for (int i = 0; i < thisDomainList.size(); ++i) {
				String thisDomain = thisDomainList.get(i);
				Integer thisLine = uuidMapLine.get(thisUUID);
				if (lineMapDomainSet.containsKey(thisLine)){
					HashSet<String> thisSet = lineMapDomainSet.get(thisLine);
					thisSet.add(thisDomain);
				}else{
					HashSet<String> thisSet = new HashSet<String>();
					thisSet.add(thisDomain);
					lineMapDomainSet.put(thisLine, thisSet);
				}
			}
		}
		
//		try{
//			BufferedWriter [] bws = new BufferedWriter[index];
//			for (int i=0; i<bws.length; ++i){
//				bws[i] = new BufferedWriter(new FileWriter(new File(trainDataFileName + (i+1) + ".txt"))); 
//			}
//			BufferedReader br = new BufferedReader(new FileReader(scaleFileName));
//			String oneLine = br.readLine();
//			int lineNum = 0;
//			while (oneLine != null){
//				if (lineSet.contains(lineNum)){
//					if (lineMapDomainSet.containsKey(lineNum)){
//						HashSet<Integer> readyIndexSet = new HashSet<Integer>();
//						HashSet<String> thisDomainSet = lineMapDomainSet.get(lineNum);
//						Iterator<String> itor_domain = thisDomainSet.iterator();
//						while (itor_domain.hasNext()){
//							String thisDomain = itor_domain.next();
//							int thisIndex = domainMapIndex.get(thisDomain);
//							readyIndexSet.add(thisIndex);
//							bws[thisIndex].write((thisIndex+1) + " " + oneLine + "\n");
//						}
//						for (int i=0; i<bws.length; ++i){
//							if (readyIndexSet.contains(i)){
//								continue;
//							}
//							bws[i].write( "-1 " + oneLine + "\n"); 
//						}
//					}else{
//						for (int i=0; i<bws.length; ++i){
//							bws[i].write( "-1 " + oneLine + "\n"); 
//						}
//					}
//				}
//				oneLine = br.readLine();
//				++ lineNum;
//			}
//			for (int i=0; i<bws.length; ++i){
//				bws[i].close(); 
//			}
//		}catch (Exception e) {
//			// TODO: handle exception
//		}
//		
		

	}
	
	public static void initLineMapUUID(String filename, HashMap<String, Integer> uuidMapLine, HashMap<Integer, String> lineMapUUID){
		try {
			String content = FileDealer.getFileContent(filename);
			StringReader sr = new StringReader(content);
			BufferedReader br = new BufferedReader(sr);
			String oneLine = br.readLine();
			while (oneLine != null){
				String [] sa = oneLine.split(",");
				if (sa.length == 2){
					try{
						Integer line = Integer.parseInt(sa[0]);
						uuidMapLine.put(sa[1].trim(), line);
						lineMapUUID.put(line, sa[1].trim());
					}catch (Exception e) {
						// TODO: handle exception
					}
				}
				oneLine = br.readLine();
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

	public static void generateTestData(String mapFileName,
			String testDataFileName, String infoFileName) {
		HashMap<String, Integer> termMapID = new HashMap<String, Integer>();
		HashMap<Integer, String> IDMapTerm = new HashMap<Integer, String>();
		GramStat.initTermMap(termMapID, IDMapTerm, mapFileName);
		HashMap<String, HashMap<String, Integer>> stemMapUUID = new HashMap<String, HashMap<String, Integer>>();
		HashMap<String, HashMap<String, Integer>> UUIDMapStem = new HashMap<String, HashMap<String, Integer>>();
		HashMap<String, HashMap<String, HashSet<Integer>>> UUIDMapStemLevel = new HashMap<String, HashMap<String, HashSet<Integer>>>();
		HashMap<String, HashMap<String, NPEntity>> UUIDMapNP = new HashMap<String, HashMap<String, NPEntity>>();
		GramStat.initGramFromInfoFile(infoFileName, 1, 5, stemMapUUID,
				UUIDMapStem, UUIDMapStemLevel, UUIDMapNP);

		File file = new File(testDataFileName);
		try {
			BufferedWriter bw = new BufferedWriter(new FileWriter(file));
			int count = 0;
			Iterator<String> itor_uuid = UUIDMapStem.keySet().iterator();
			while (itor_uuid.hasNext()) {
				String thisUUID = itor_uuid.next();
				HashMap<String, Integer> stemMapCount = UUIDMapStem
						.get(thisUUID);
				if (stemMapCount != null) {
					ArrayList<TrainTerm> termList = new ArrayList<TrainTerm>();
					Iterator<String> itor_stem = stemMapCount.keySet()
							.iterator();
					while (itor_stem.hasNext()) {
						String thisStem = itor_stem.next();
						if (termMapID.containsKey(thisStem)) {
							TrainTerm thisTerm = new TrainTerm();
							thisTerm.setContent(thisStem);
							thisTerm.setCount(stemMapCount.get(thisStem));
							thisTerm.setId(termMapID.get(thisStem));
							termList.add(thisTerm);
						}
					}
					Collections.sort(termList);
					if (termList.size() == 0) {
//						System.out.println("no data:" + thisUUID);
						continue;
					}
//					bw.write(1 + " ");
					// HashSet<Integer> idSet = new HashSet<Integer>();
					System.out.println(count + "," + thisUUID);
					HashMap<Integer, TrainTerm> idMapTerm = new HashMap<Integer, TrainTerm>();
					for (int j = 0; j < termList.size(); ++j) {
						idMapTerm.put(termList.get(j).getId(), termList.get(j));
						// bw.write(" " + termList.get(j).getId() + ":" +
						// termList.get(j).getCount());
					}
					for (int j = 0; j < termMapID.size(); ++j) {
						if (j > 0){
							bw.write(" ");
						}
						if (idMapTerm.containsKey(j + 1)) {
							bw.write((j + 1) + ":"
									+ idMapTerm.get(j + 1).getCount());
						} else {
							bw.write((j + 1) + ":" + 0);
						}
					}
					bw.write("\n");

					++count;
				} else {
//					System.out.println("error:" + thisUUID);
				}
			}

			bw.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public static void generateTrainData(String manualFileName,
			String mapFileName, String trainDataFileName, String infoFileName) {
		HashMap<String, ArrayList<String>> uuidMapFunc = new HashMap<String, ArrayList<String>>();
		HashMap<String, ArrayList<String>> uuidMapDomain = new HashMap<String, ArrayList<String>>();
		ManualTagReader
				.readTag(manualFileName, 150, uuidMapFunc, uuidMapDomain);
		HashMap<String, Integer> termMapID = new HashMap<String, Integer>();
		HashMap<Integer, String> IDMapTerm = new HashMap<Integer, String>();
		GramStat.initTermMap(termMapID, IDMapTerm, mapFileName);
		HashSet<String> domainSet = new HashSet<String>();
		Iterator<String> itor_uuid = uuidMapDomain.keySet().iterator();
		while (itor_uuid.hasNext()) {
			String thisUUID = itor_uuid.next();
			ArrayList<String> thisDomainList = uuidMapDomain.get(thisUUID);
			for (int i = 0; i < thisDomainList.size(); ++i) {
				domainSet.add(thisDomainList.get(i));
			}
		}
		Iterator<String> itor_domain = domainSet.iterator();
		HashMap<String, ArrayList<String>> domainMapUUID = new HashMap<String, ArrayList<String>>();
		while (itor_domain.hasNext()) {
			String thisDomain = itor_domain.next();
			domainMapUUID.put(thisDomain, new ArrayList<String>());
			// System.out.println(thisDomain);
		}
		itor_uuid = uuidMapDomain.keySet().iterator();
		while (itor_uuid.hasNext()) {
			String thisUUID = itor_uuid.next();
			ArrayList<String> thisDomainList = uuidMapDomain.get(thisUUID);
			for (int i = 0; i < thisDomainList.size(); ++i) {
				String thisDomain = thisDomainList.get(i);
				ArrayList<String> thisUUIDList = domainMapUUID.get(thisDomain);
				thisUUIDList.add(thisUUID);
			}
		}

		HashMap<String, HashMap<String, Integer>> stemMapUUID = new HashMap<String, HashMap<String, Integer>>();
		HashMap<String, HashMap<String, Integer>> UUIDMapStem = new HashMap<String, HashMap<String, Integer>>();
		HashMap<String, HashMap<String, HashSet<Integer>>> UUIDMapStemLevel = new HashMap<String, HashMap<String, HashSet<Integer>>>();
		HashMap<String, HashMap<String, NPEntity>> UUIDMapNP = new HashMap<String, HashMap<String, NPEntity>>();
		GramStat.initGramFromInfoFile(infoFileName, 1, 5, stemMapUUID,
				UUIDMapStem, UUIDMapStemLevel, UUIDMapNP);

		try {

			itor_domain = domainSet.iterator();
			int index = 1;
			Set<String> totalUUIDSet = uuidMapDomain.keySet();
			totalUUIDSet.remove("5e586832-6157-4dc1-81fa-7d98296c5265");
			totalUUIDSet.remove("e9dd5269-ac0f-4266-85da-2809f309e2df");
			totalUUIDSet.remove("b21a2f54-baed-4406-87b0-f952129b3884");
			totalUUIDSet.remove("5e586832-6157-4dc1-81fa-7d98296c5265");
			totalUUIDSet.remove("9c73abe0-e336-4db5-8d9f-27dc5c11eff8");

			while (itor_domain.hasNext()) {
				String thisDomain = itor_domain.next();
				System.out.println(thisDomain);
				ArrayList<String> uuidList = domainMapUUID.get(thisDomain);
				Collections.sort(uuidList);
				File file = new File(trainDataFileName + index + ".txt");
				BufferedWriter bw = new BufferedWriter(new FileWriter(file));
				HashSet<String> readyUUIDSet = new HashSet<String>();
				for (int i = 0; i < uuidList.size(); ++i) {
					String thisUUID = uuidList.get(i);
					HashMap<String, Integer> stemMapCount = UUIDMapStem
							.get(thisUUID);
					if (stemMapCount != null) {
						ArrayList<TrainTerm> termList = new ArrayList<TrainTerm>();
						Iterator<String> itor_stem = stemMapCount.keySet()
								.iterator();
						while (itor_stem.hasNext()) {
							String thisStem = itor_stem.next();
							if (termMapID.containsKey(thisStem)) {
								TrainTerm thisTerm = new TrainTerm();
								thisTerm.setContent(thisStem);
								thisTerm.setCount(stemMapCount.get(thisStem));
								thisTerm.setId(termMapID.get(thisStem));
								termList.add(thisTerm);
							}
						}
						Collections.sort(termList);
						if (termList.size() == 0) {
							System.out.println("no data:" + thisUUID);
							continue;
						}
						bw.write(1 + " ");
						// HashSet<Integer> idSet = new HashSet<Integer>();
						HashMap<Integer, TrainTerm> idMapTerm = new HashMap<Integer, TrainTerm>();
						for (int j = 0; j < termList.size(); ++j) {
							idMapTerm.put(termList.get(j).getId(), termList
									.get(j));
							// bw.write(" " + termList.get(j).getId() + ":" +
							// termList.get(j).getCount());
						}
						for (int j = 0; j < termMapID.size(); ++j) {
							if (j>0){
								bw.write(" ");
							}
							if (idMapTerm.containsKey(j + 1)) {
								bw.write((j + 1) + ":"
										+ idMapTerm.get(j + 1).getCount());
							} else {
								bw.write((j + 1) + ":" + 0);
							}
						}
						bw.write("\n");
						readyUUIDSet.add(thisUUID);
					} else {
						System.out.println("error:" + thisUUID);
					}

				}
				itor_uuid = totalUUIDSet.iterator();
				while (itor_uuid.hasNext()) {
					String thisUUID = itor_uuid.next();
					if (!readyUUIDSet.contains(thisUUID)) {
						HashMap<String, Integer> stemMapCount = UUIDMapStem
								.get(thisUUID);
						if (stemMapCount != null) {
							ArrayList<TrainTerm> termList = new ArrayList<TrainTerm>();
							Iterator<String> itor_stem = stemMapCount.keySet()
									.iterator();
							while (itor_stem.hasNext()) {
								String thisStem = itor_stem.next();
								if (termMapID.containsKey(thisStem)) {
									TrainTerm thisTerm = new TrainTerm();
									thisTerm.setContent(thisStem);
									thisTerm.setCount(stemMapCount
											.get(thisStem));
									thisTerm.setId(termMapID.get(thisStem));
									termList.add(thisTerm);
								}
							}
							Collections.sort(termList);
							if (termList.size() == 0) {
								System.out.println("no data:" + thisUUID);
								continue;
							}
							bw.write("-1 ");
							// HashSet<Integer> idSet = new HashSet<Integer>();
							HashMap<Integer, TrainTerm> idMapTerm = new HashMap<Integer, TrainTerm>();
							for (int j = 0; j < termList.size(); ++j) {
								idMapTerm.put(termList.get(j).getId(), termList
										.get(j));
								// bw.write(" " + termList.get(j).getId() + ":"
								// + termList.get(j).getCount());
							}
							for (int j = 0; j < termMapID.size(); ++j) {
								if (idMapTerm.containsKey(j + 1)) {
									bw.write(" " + (j + 1) + ":"
											+ idMapTerm.get(j + 1).getCount());
								} else {
									bw.write(" " + (j + 1) + ":" + 0);
								}
							}
							bw.write("\n");
							// readyUUIDSet.add(thisUUID);
						} else {
							System.out.println("error:" + thisUUID);
						}
					}
				}
				bw.close();
				++index;
			}

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
}
